package com.zb.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zb.entity.Car;
import com.zb.entity.Help;
import com.zb.entity.Mr;
import com.zb.entity.Yh;
import com.zb.service.UserService;
import com.zb.service.YhService;
import com.zb.serviceimpl.UserServiceImpl;
import com.zb.serviceimpl.YhServiceImpl;
import com.zb.util.PageUtil;

/**
 * Servlet implementation class YhServlet
 */
public class YhServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
   
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		String op = request.getParameter("op");
		if(op.equals("show")) {
			this.doshow(request, response);
		}else if(op.equals("toupdate")) {
			this.toupdate(request, response);
		}else if(op.equals("doupdate")) {
			this.doupdate(request, response);
		}else if(op.equals("del")) {
			this.del(request, response);
		}
	}
	protected void doshow(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		YhService yhService=new YhServiceImpl();
		int index=1;
		String carnum=null;
		if (request.getParameter("index")!=null&&!"".equals(request.getParameter("index"))) {
			index = Integer.parseInt(request.getParameter("index"));
		}
		carnum=request.getParameter("carnum");
			PageUtil<Yh> page = yhService.getPersonPage(index, carnum);
			request.setAttribute("mypage", page);
			
			
			request.getRequestDispatcher("search_yh.jsp").forward(request, response);
	}
	protected void toupdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		YhService yhService=new YhServiceImpl();
		int id = Integer.parseInt(request.getParameter("id"));
		
		Yh m = yhService.getYhById(id);
		request.setAttribute("p",m);
		request.getRequestDispatcher("Yhupdate.jsp").forward(request, response);
	}
	protected void doupdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int id=Integer.parseInt(request.getParameter("id"));
		String StickExp=request.getParameter("stickexp");
		String stickname=request.getParameter("stickname");
		String stickdate=request.getParameter("stickdate");
		String money=request.getParameter("money");
		String remake=request.getParameter("remake");
		YhService yhService=new YhServiceImpl();
		Yh y=new Yh();
		Car c=new Car();
		c.setId(id);
		y.setStickExp(StickExp);
		y.setStickDate(stickdate);
		y.setStickMoney(money);
		y.setRemake(remake);
		y.setCar(c);
		yhService.update(y);
		response.sendRedirect("Yh.do?op=show");
	}
	protected void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int nid= Integer.parseInt(request.getParameter("id"));
		YhService yhService=new YhServiceImpl();
		yhService.delYh(nid);
		response.sendRedirect("Yh.do?op=show");
	}
}
